package com.cookie.framework.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.cookie.framework.R;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.framework.view
 * 文件名:    TouchPictureV
 * 创建者:    cookie
 * 创建时间:   2019/12/31 23:13
 * 描述:      图片验证View
 */

public class TouchPictureV extends View {

    //背景
    private Bitmap bgBitmap;
    //画笔
    private Paint paintBg;
    //空白块
    private Bitmap nullBitmap;
    //空白块画笔
    private Paint paintNull;
    //移动方块
    private Bitmap moveBitmap;
    //移动画笔
    private Paint paintMove;
    //View的宽高
    private int width;
    private int height;
    //方块大小
    private int CARD_SIZE = 200;
    //方块坐标
    private int LINE_W, LINE_H = 0;
    //移动方块横坐标
    private int moveX = 200;
    //误差值
    private int errorValues = 10;

    private OnViewResultListener viewResultListener;

    public void setOnViewResultListener(OnViewResultListener viewResultListener) {
        this.viewResultListener = viewResultListener;
    }

    public TouchPictureV(Context context) {
        super(context);
        init();
    }

    public TouchPictureV(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchPictureV(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paintBg = new Paint();
        paintNull = new Paint();
        paintMove = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBg(canvas);
        drawNullCard(canvas);
        drawMoveCard(canvas);
    }

    //绘制移动的方块
    private void drawMoveCard(Canvas canvas) {
        //截取空白块位置坐标的Bitmap图像
        moveBitmap = Bitmap.createBitmap(bgBitmap, LINE_W, LINE_H, CARD_SIZE, CARD_SIZE);
        //绘制在View上 如果LINE_W,LINE_H,那会和空白块重叠
        canvas.drawBitmap(moveBitmap, moveX, LINE_H, paintMove);
    }

    //绘制空白块
    private void drawNullCard(Canvas canvas) {
        //获取图片
        nullBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_null_card);
        //计算值
        CARD_SIZE = nullBitmap.getWidth();
        //99 / 3 = 33 * 2 = 66
        LINE_W = width / 3 * 2;
        //除以2并不是中心
        LINE_H = height / 2 - (CARD_SIZE / 2);
        //绘制
        canvas.drawBitmap(nullBitmap, LINE_W, LINE_H, paintNull);
    }

    //绘制背景
    private void drawBg(Canvas canvas) {
        //获取图片
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_bg);
        //创建一个空的Bitmap Bitmap w h = View w h
        bgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //将图片绘制到空的Bitmap
        Canvas bgCanvas = new Canvas(bgBitmap);
        bgCanvas.drawBitmap(bitmap, null, new Rect(0, 0, width, height), paintBg);
        //将bgBitmap绘制到View上
        canvas.drawBitmap(bgBitmap, null, new Rect(0, 0, width, height), paintBg);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //判断点击的坐标是否是方块的内部，如果是就可以拖动
                break;
            case MotionEvent.ACTION_MOVE:
                //防止越界
                if (event.getX() > 0 && event.getX() < (width - CARD_SIZE)) {
                    moveX = (int) event.getX();
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                //验证结果
                if (moveX > (LINE_W - errorValues) && moveX < (LINE_W + errorValues)) {
                    if (viewResultListener != null) {
                        viewResultListener.onResult();
                        //重置
                        moveX = 200;
                        invalidate();
                    }
                }
                break;
        }
        return true;
    }

    public interface OnViewResultListener {
        void onResult();
    }
}
